<div class="mt15 text-center">
    <button type="button" class="btn btn-success mt5" id="printCart">
        <i class="iconfont icon-dayin"></i>打印个体户营业执照正副本&emsp;
    </button>
</div>
<input type="hidden" id="dialog" name="dialog" value="">
<script src="/static/start/modules/main.js"></script>
<script src="/static/start/modules/A4Print.js"></script>
<script>
    require(['base', 'msgTool', "plugins/bootstrap-dialog/bootstrap-dialog", "plugins/bootstrap-table/bootstrap-table"], function (base, msgTool, BootstrapDialog) {

        base();
        /**
         * 打印正副本
         */
        $("#printCart").on('click', function () {
            doPrint();
        });


        /**
         * 实际打印方法

         */
        function doPrint() {

            if (window.isAIO !== undefined && window.isAIO) {
                $.ajax({
                    url: "/start/print/${idCardNo}/api",
                    method: "POST",
                    dataType: "json",
                    data: {
                        id: '${id}',
                        idCardNo: '${idCardNo}',
                        receiveUserName: localStorage.getItem("userName")
                    },
                    beforeSend: function () {
                        $("#printCart").text("打印中...").attr("disabled", "disabled");
                    },
                    success: function (data) {
                        if (data.success) {
                            //正本
                            print(1, 2, 1);
                            //副本
                            print(2, 1, 0);
                            //关闭弹窗
                            $.each(BootstrapDialog.dialogs, function (id, dialogs) {
                                if (id === $("#dialog").val()) {
                                    dialogs.close();
                                }
                            });

                        } else {
                            msgTool.error("警告，" + data.msg, "错误提示");
                        }
                    },
                    error: function (jqXHR) {
                        msgTool.error(jqXHR.status + " (" + jqXHR.statusText + ")", "错误提示");
                    },
                    complete: function () {
                        $("#printCart").html("<i class=\"iconfont icon-dayin\"></i>打印个体户营业执照正副本&emsp;").removeAttr("disabled");
                        $('#dataList').bootstrapTable('refresh');
                    }
                });
            } else {
                msgTool.error("警告，当前不是一体机环境", "错误提示");
            }
        }

        /**
         * 实际打印方法
         * @param certType 证件类型，1正，2副本
         * @param choiceTray 打印纸盒选择（0、使用默认纸盒，1、使用纸盒1，2、使用纸盒2，5、使用手动纸盒）
         * @param choicePaper 打印纸张选择（0:A4，1:A3）
         */

        function print(certType, choiceTray, choicePaper) {
            var printData = JSON.stringify({
                "url": "${webUtil.domain(request)}start/merchant/show/${idCardNo}/${id}/" + certType,
                "ChoiceTray": choiceTray,
                "ChoicePaper": choicePaper
            });
            var msgText = certType === 1 ? "个体户营业执照正本" : "个体户营业执照副本";
            send_command(
                "printUrl2",
                printData,
                function (receivedData) {
                    var info = JSON.parse(receivedData);
                    if (info.status === 100) {
                        msgTool.success("恭喜您，打印" + msgText + "成功！", "信息提示");
                    }
                    else if (info.status === 300) {
                        msgTool.error("模块未开启", "错误提示");
                    }
                    else {
                        msgTool.error("打印失败，" + info.msg, "错误提示");
                    }
                }
            );
        }

    })
    ;
</script>